package com.example.a17376080_dlz_finaltest;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.support.v7.widget.Toolbar;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import adapter.Comic;
import adapter.MajorAdapter;
import okhttp3.Call;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import utils.HttpUtil;


public class Major_Fragment extends Fragment {
    private Context context;
    private SwipeRefreshLayout swipeRefresh;
    MajorAdapter adapter;
    private static final int UPDATA_TXT=1;
    View view;
    TextView tv_sign;
    Comic comic;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case UPDATA_TXT:
                    adapter.notifyDataSetChanged();
                    swipeRefresh.setRefreshing(false);
                    break;

            }
        }
    };
    private static List<Comic> comicList=new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.major_fragment,null);

        //toolBar
//       Toolbar toolbar=view.findViewById(R.id.toolbar);
//      ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
       //toolbar.inflateMenu(R.menu.menu_major);

        context=getContext();
        RecyclerView recyclerView=view.findViewById(R.id.recycle_view);
        LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        adapter=new MajorAdapter(comicList,context);
        recyclerView.setAdapter(adapter);
        if(comicList==null||comicList.isEmpty()){
            startOkHttp();
        }
        startSwipRefresh();
        return view;
    }

    private void startSwipRefresh() {
        swipeRefresh=view.findViewById(R.id.swipe_refeesh);
        swipeRefresh.setColorSchemeResources(R.color.colorPrimary);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if(comicList==null||comicList.isEmpty()){
                                startOkHttp();
                                //刷新数据
                            }else {
                                swipeRefresh.setRefreshing(false);
                            }
                            Thread.sleep(2000);
                          //  adapter.notifyDataSetChanged();
                           // swipeRefresh.setRefreshing(false);

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }
        });
    }

    private void startOkHttp() {
        HttpUtil.sendOkHttpRequest("http://106.14.221.71:8080/Service_dlz/comic_findAll.action",new okhttp3.Callback(){
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseData=response.body().string();
                Log.d("回来的数据：", responseData);
                //GSON解析
                parseJSONWithGSON(responseData);
                Message message=new Message();
                message.what=UPDATA_TXT;
                handler.sendMessage(message);

            }

            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("没有得到信息", "onFailure: ");
            }
        });

                     // .url("http://101.132.110.172:8080/AndroidDataFinalTest/commodity_findAll.action")


    }

    private void parseJSONWithGSON(String jsonData) {
        Gson gson=new Gson();
        comicList.addAll((Collection<? extends Comic>) gson.fromJson(jsonData,new TypeToken<List<Comic>>(){}.getType()));


    }

}
